package com.me.videoservice.mqtt;

import com.me.videoservice.service.MqttService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;

@Slf4j
public class MqttMessageHandler implements MessageHandler {

  @Autowired
  private List<MqttService> mqttServices;

  @Override
  public void handleMessage(Message<?> message) throws MessagingException {
    String topic = (String) message.getHeaders().get("mqtt_receivedTopic");
    try {
      mqttServices.stream()
          .filter(it -> it.topic((topic)))
          .forEach(it -> it.process(message, topic));
    } catch (Exception e) {
      log.error("mqtt执行失败",e);
    }
  }

}
